package main

import (
	"fmt"
	"strings"

	"main/role"
)

type Player struct {
	ID   int       // 座位号从 1 开始
	Role role.Role // 身份
}

func (p *Player) Villager() *role.Villager {
	return p.Role.(*role.Villager)
}

func (p *Player) Werewolf() *role.Werewolf {
	return p.Role.(*role.Werewolf)
}

func (p *Player) Witch() *role.Witch {
	return p.Role.(*role.Witch)
}

func (p *Player) Seer() *role.Seer {
	return p.Role.(*role.Seer)
}

func (p *Player) Hunter() *role.Hunter {
	return p.Role.(*role.Hunter)
}

type Players []*Player

func (p Players) GetVillagers() Players {
	players := make(Players, 0)
	for _, player := range p {
		if player.Role.GetRole() == role.VillagerID {
			players = append(players, player)
		}
	}
	return players
}

func (p Players) GetWerewolves() Players {
	players := make(Players, 0)
	for _, player := range p {
		if player.Role.GetRole() == role.WerewolfID {
			players = append(players, player)
		}
	}
	return players
}

func (p Players) GetGods() Players {
	players := make(Players, 0)
	for _, player := range p {
		if player.Role.GetRole() == role.WitchID ||
			player.Role.GetRole() == role.SeerID ||
			player.Role.GetRole() == role.HunterID {
			players = append(players, player)
		}
	}
	return players
}

func (p Players) GetWitch() *Player {
	for _, player := range p {
		if player.Role.GetRole() == role.WitchID {
			return player
		}
	}
	return nil
}

func (p Players) GetSeer() *Player {
	for _, player := range p {
		if player.Role.GetRole() == role.SeerID {
			return player
		}
	}
	return nil
}

func (p Players) GetHunter() *Player {
	for _, player := range p {
		if player.Role.GetRole() == role.HunterID {
			return player
		}
	}
	return nil
}

func (p Players) GetAlive() Players {
	players := make(Players, 0)
	for _, player := range p {
		if player.Role.IsAlive() {
			players = append(players, player)
		}
	}
	return players
}

func (p Players) GetIDString() string {
	builder := strings.Builder{}
	for i, player := range p {
		builder.WriteString(fmt.Sprintf("%d号", player.ID))
		if i != len(p)-1 {
			builder.WriteString("，")
		}
	}
	return builder.String()
}
